package cn.tedu.eguser1.controller;

import cn.tedu.eguser1.base.response.JsonResult;
import cn.tedu.eguser1.base.response.StatusCode;
import cn.tedu.eguser1.mapper.UserMapper;
import cn.tedu.eguser1.pojo.dto.UserAddParam;
import cn.tedu.eguser1.pojo.dto.UserUpdateParam;
import cn.tedu.eguser1.pojo.entity.User;
import cn.tedu.eguser1.pojo.vo.UserListVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
@Slf4j
@RestController
@RequestMapping("/v1/user/")
public class UserController {
    @Autowired
    private UserMapper userMapper;

    /**
     * 添加用户
     */
    @PostMapping("add")
    public JsonResult add(UserAddParam userAddParam){
        log.debug("userAddParam = "+userAddParam);
        User user = new User();
        BeanUtils.copyProperties(userAddParam,user);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());

        int i=userMapper.insert(user);
        if(i>0){
            return JsonResult.ok();
        }
        return new JsonResult(StatusCode.OPERATION_FAILED);

    }

    /**
     * 查询用户列表
     */
    @GetMapping("list")
    public JsonResult list(){
        List<UserListVO> list=
                userMapper.SelectAll();
        log.debug("list = "+list);
        return JsonResult.ok(list);
    }

    /**
     * 删除用户
     */
    @PostMapping("del")
    public JsonResult del(Integer id){
        log.debug("id = "+id);
        int i=userMapper.deleteById(id);
        if (i>0){
            return JsonResult.ok();
        }
        return new JsonResult(StatusCode.OPERATION_FAILED);
    }

    /**
     * 更新用户
     */
    @PostMapping("update")
    public JsonResult update(UserUpdateParam userUpdateParam){
        log.debug("userUpdateParam = " +userUpdateParam);
        User user = new User();
        BeanUtils.copyProperties(userUpdateParam,user);
        user.setUpdateTime(new Date());
        int i=userMapper.UpdateById(user);

        if (i>0){
            return JsonResult.ok();
        }
        return new JsonResult(StatusCode.OPERATION_FAILED);
    }





















}
